<template>
  <div class="dialog-form">
    <el-dialog v-model="formVisible" class="elDialogBox" title="新增执行记录" @close="onClickCloseBtn">
      <!-- 表单内容 -->
      <div class="dialogCenter">
        <el-form ref="queryRef" :model="formData" :rules="rules" label-width="108px">
          <el-form-item label="执行时间：" name="estimatedServerTime">
            <el-date-picker v-model="formData.estimatedServerTime" :default-time="defaultTime" placeholder=""
                            type="datetime"/>
          </el-form-item>
          <el-form-item label="执行图片：" name="taskImage">
            <image-upload v-model="formData.taskImage"/>
          </el-form-item>
          <el-form-item label="执行记录：" name="mark">
            <el-input v-model="formData.mark" :maxlength="100" class="wt-400" placeholder="请输入" resize="none"
                      show-word-limit type="textarea">
            </el-input>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="onClickCloseBtn">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts" setup>
import {ref, watch} from 'vue';
import dayjs from 'dayjs';

const {proxy} = getCurrentInstance();

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
});

const emit: Function = defineEmits(['handleClose', 'handleSubmit']);
const resetType = ref('empty');
const form = ref();
const formVisible = ref(false);

const formData = ref({
  estimatedServerTime: '',
  mark: '',
});
const autoUpload = ref(true);
const photoFile = ref([]);
const sizeLimit = ref({
  size: 2,
  unit: 'MB',
  message: '图片大小超过2m，请重新上传',
});

const defaultTime = new Date(2000, 1, 1, 12, 0, 0);

const rules = {
  estimatedServerTime: [
    {
      required: true,
      message: '执行时间为空，请选择执行时间',
      trigger: 'change',
    }
  ],
  taskImage: [
    {
      required: true,
      message: '执行图片为空，请上传执行图片',
      trigger: 'change',
    }
  ],
  mark: [
    {
      required: true,
      message: '执行记录为空，请输入执行记录',
      trigger: 'blur',
    }
  ]
};

watch(
    () => props.visible,
    (val) => {
      formVisible.value = props.visible;
      if (val) {
        formData.value.estimatedServerTime = dayjs().format(
            'YYYY-MM-DD HH:mm:ss'
        );
      }
    }
);

// 提交表单
const submitForm = () => {
  proxy.$refs['queryRef'].validate((valid) => {
    if (valid) {
      emit('handleSubmit', formData.value);
      handleClear();
    }
  });
};

// 获取时间
const handleChange = (date) => {
  formData.value.estimatedServerTime = dayjs(date).valueOf();
};

// 清除表单数据
const handleClear = () => {
  proxy.resetForm('queryRef');
  formData.value.mark = '';
  formData.value.estimatedServerTime = '';
  photoFile.value = [];
  formData.value.taskImage = '';
};

const onClickCloseBtn = () => {
  handleClear();
  emit('handleClose');
};

const remove = () => {
  photoFile.value = [];
  formData.value.taskImage = '';
};

const handleFail = () => {
  MessagePlugin.error(`文件上传失败`);
};

const handleSuccess = (params) => {
  const photo = params.response.data;
  formData.value.taskImage = photo;
  photoFile.value[0].response.url = photo;
  photoFile.value[0].url = photo;
};

const beforeUpload = (file) => {
  if (file.size > 2 * 1024 * 1024) {
    MessagePlugin.error('图片大小超过2M，请重新上传');
    return false;
  }
  return true;
};
defineExpose({
  handleClear,
});
</script>
<style lang="scss" scoped>
.expectTime {
  width: 400px;
}
</style>
